<?php

namespace app\admin\controller\baobiao;

use app\admin\model\pro\Birth;
use app\common\controller\Backend;
use think\Db;
use think\Session;
/**
 * 消毒类型
 *
 * @icon fa fa-circle-o
 */
class Produce extends Backend
{

    /**
     * Type模型对象
     * @var
     */
    protected $model = null;

    //  大厂权限      @wtt
    protected $dataLimitCom = "auth";       // 默认: false,  'auth':  强制验证厂区权限 ( 表示显示当前厂区数据 )
    protected $dataCompanyField = "factory_id";     // 厂区权限字段

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Batch;
    }

    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();

        //  todo 131
        //  加  “窝均带仔数”   =选留数/选留窝数

        $list = $this->model
            ->where($where)
            ->with('Birth')->with('Breed')->with('Daily')->with('Disinfect')->with('Ears')->with('Light')->with('Milk')->with('Milkout')
            ->with('Milkpre')->with('Out')->with('Select')->with('Sex')->with('Touch')->with('Weight')
            ->order($sort, $order)
            ->paginate($limit);

        $lists = $this->getXXX($list->items());

        foreach($lists as $k => $val){
            $ideal = Db::name("batchschedule")->where("batch_id", $val['id'] )->where("deletetime", null )->find();
            if($ideal) {
                $lists[$k]['jiaguangriqi'] = $ideal['light'];       // 理论日期
                $lists[$k]['peizhongriqi'] = $ideal['breed'];
                $lists[$k]['motairiqi'] = $ideal['touch'];
                $lists[$k]['chanzairiqi'] = $ideal['birth'];
                $lists[$k]['duannairiqi'] = $ideal['milkout'];
                $lists[$k]['chulanriqi'] = $ideal['out'];
            }

            # 显示报表字段
            $lists[$k]['a1'] = $val['light'][0]['num'];
            $lists[$k]['a2'] = $val['light'][0]['zhongtu_update'];      // 剩余母兔+补充母兔
            $lists[$k]['b1'] = $val['breed'][0]['num'];
            $lists[$k]['c1'] = $val['touch'][0]['snum'];
            $lists[$k]['c2'] = $val['touch'][0]['rate'] ? $val['touch'][0]['rate'] * 100 . "%" : "";

            // 产仔窝数//产仔数//产仔率//窝均产仔数//产活仔数//窝均产活仔数
            if($val['birth'][0]) {
                $lists[$k]['d1'] = $val['birth'][0]['num'] ;
                $lists[$k]['d2'] = $val['birth'][0]['cnum'] ;
                $lists[$k]['d3'] = $val['birth'][0]['rate'] ;
                $lists[$k]['d4'] = $val['birth'][0]['every'] ;
                $lists[$k]['d5'] = $val['birth'][0]['lnum'] ;
                $lists[$k]['d6'] = $val['birth'][0]['lrate'] ;
            }
            if($val['select'][0]) {
                $lists[$k]['e1'] = $val['select'][0]['nnum'] ;
                $lists[$k]['e2'] = $val['select'][0]['num'] ;
                $lists[$k]['e3'] = "";
            }
            if($val['milkout'][0]) {
                $lists[$k]['f1'] = $val['milkout'][0]['num'] ;
                $lists[$k]['f2'] = $val['milkout'][0]['rate'] ;
                $lists[$k]['f3'] = $val['milkout'][0]['every'];
                $lists[$k]['f4'] = "";
                $lists[$k]['f5'] = "";
            }
            if($val['out'][0]) {
                $lists[$k]['g1'] = "";
                $lists[$k]['g2'] = $val['out'][0]['num'] ;
                $lists[$k]['g3'] = $val['out'][0]['every'];
                $lists[$k]['g4'] = $val['out'][0]['nnum'];
                $lists[$k]['g5'] = $val['out'][0]['bnum'];
            }
        }

// dump($list);die;
        $result = ['total' => $list->total(), 'rows' => $lists ];
        return json($result);
    }


    public function index0(){
        //        $list = Db::name("batch")
        //            ->alias('a')
        //            ->join("pro_birth b","a.id = b.batch_id",'left')//产仔
        //            ->join("pro_breed br","a.id = br.batch_id",'left')//配种
        //            ->join("pro_light l","a.id = l.batch_id",'left')//加光母兔数
        //            ->join("pro_milkout mo","a.id = mo.batch_id",'left')//断奶
        //            ->join("pro_out o","a.id = o.batch_id",'left')//出栏
        //            ->join("pro_select sl","a.id = sl.batch_id",'left')//选留
        //            ->join("pro_touch t","a.id = t.batch_id",'left')//摸胎
        //            ->join("pro_weight w","a.id = w.batch_id",'left')
        //            //->join("group g","a.group_id = g.id",'left')
        //            //->join("factory f","a.factory_id = f.id",'left')
        //            ->field("a.*,
        //            b.newdate as chanzairiqi,b.num as chanzaiwoshu,b.cnum as chanzaizongshu,b.every as wojun,b.rate as chanzailv,b.lnum as chenghuo,b.lrate as wojunhuozai,
        //            br.num as peizhongmutu,br.rate as peizhonglv,br.light_num,br.newdate peizhongriqi,
        //            l.num as jiaguanshu,l.newdate as jiaguangriqi,
        //            mo.newdate as duannairiqi,mo.num as duannaizongshu,mo.cnum as duannaiwoshu,mo.every as duannaichenghuo,mo.rate as duannaijunzhong,
        //            o.newdate as chulanriqi,o.num as chulanzongshu,o.nnum as chulanzhongtu,o.bnum as chulanshangpin,o.every as chulanjunzhong,
        //            sl.num as xuanliushu,sl.nnum as xuanliuwoshu,
        //            t.num as motaishu,t.newdate as motairiqi,t.snum as shoutaishu,t.rate as shoutailv
        //            ")
        //            ->where($where)
        //            ->order($sort, $order)
        //            ->paginate($limit);

        }

}
